﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using System.Diagnostics;

namespace AccessDBUtilDemo
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		private int curPageIndex;
		private int totalPage;
		private ListViewItem curLvi;
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			curPageIndex =0;
			Page page = PageQueryDao.getProducts(curPageIndex);
			List<Product> ls = (List<Product>)page.ValueList;
			//init product  list view
			foreach(Product p in ls)
			{
				AddProduct(p);
			}
			
			this.labelMsg.Text=String.Format("记录数: {0:d}, \n每页{1:d}条记录, {2:d}/{3:d}页",
			                            page.TotalRecord,page.PageSize,page.CurPageIndex,page.TotalPage);
			totalPage = page.TotalPage;
		}
		
		
		void ButtonExitClick(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void ButtonAddClick(object sender, EventArgs e)
		{
			//
			StringBuilder strErrorMsg = new StringBuilder();
			if(!Validator.checkRequired(this.textBoxName.Text))
			{
				strErrorMsg.Append("名称不能为空\n");
			}
			if(!Validator.checkPositiveInteger(this.textBoxQuantity.Text))
			{
				strErrorMsg.Append("数量必须是正整数或零\n");
			}
			if(!Validator.checkDouble(this.textBoxPrice.Text))
			{
				strErrorMsg.Append("单价必须是浮点数\n");
			}
			if(strErrorMsg.Length>0)
			{
				MessageBox.Show(strErrorMsg.ToString(),"出错提示");
				return;
			}
			//
			Product p= new Product();
			p.Name = this.textBoxName.Text.Trim();
			p.Quantity = Int32.Parse(this.textBoxQuantity.Text.Trim());
			p.Price = Double.Parse(this.textBoxPrice.Text.Trim());
			p.SaleDate=this.dateTimePicker1.Value;
			p.CheckedFlag=this.radioButtonChecked.Checked;
			int id = ProductDao.insert(p);
			this.textBoxId.Text=id.ToString();
			//
			p = ProductDao.getById(id);
			AddProduct(p);			
		}
		private void AddProduct(Product p)
		{
			ListViewItem lvi = this.listView1.Items.Add(p.Id.ToString());
			lvi.SubItems.Add(p.Name);
			lvi.SubItems.Add(p.Quantity.ToString());
			lvi.SubItems.Add(p.Price.ToString());
			lvi.SubItems.Add(p.SaleDate.ToString());
			lvi.SubItems.Add(p.CheckedFlag.ToString());
		}
		
		void ButtonFirstClick(object sender, EventArgs e)
		{
			this.listView1.Items.Clear();
			Page page = PageQueryDao.getProducts(1);
			List<Product> ls = (List<Product>)page.ValueList;
			foreach(Product p in ls)
			{
				AddProduct(p);
				//Debug.WriteLine(p.Id);
			}
			
			this.labelMsg.Text=String.Format("记录数: {0:d}, \n每页{1:d}条记录, {2:d}/{3:d}页",
			                            page.TotalRecord,page.PageSize,page.CurPageIndex,page.TotalPage);
			
			curPageIndex=page.CurPageIndex;
			totalPage = page.TotalPage;
		}
		
		void ButtonPrevClick(object sender, EventArgs e)
		{
			this.listView1.Items.Clear();
			curPageIndex--;
			Page page = PageQueryDao.getProducts(curPageIndex);
			List<Product> ls = (List<Product>)page.ValueList;
			foreach(Product p in ls)
			{
				AddProduct(p);
				//Debug.WriteLine(p.Id);
			}
			
			this.labelMsg.Text=String.Format("记录数: {0:d}, \n每页{1:d}条记录, {2:d}/{3:d}页",
			                            page.TotalRecord,page.PageSize,page.CurPageIndex,page.TotalPage);
			
			curPageIndex=page.CurPageIndex;
			totalPage = page.TotalPage;
		}
		
		void ButtonNextClick(object sender, EventArgs e)
		{
			this.listView1.Items.Clear();
			curPageIndex++;
			Page page = PageQueryDao.getProducts(curPageIndex);
			List<Product> ls = (List<Product>)page.ValueList;
			foreach(Product p in ls)
			{
				AddProduct(p);
				//Debug.WriteLine(p.Id);
			}
			
			this.labelMsg.Text=String.Format("记录数: {0:d}, \n每页{1:d}条记录, {2:d}/{3:d}页",
			                            page.TotalRecord,page.PageSize,page.CurPageIndex,page.TotalPage);
			
			curPageIndex=page.CurPageIndex;
			totalPage = page.TotalPage;
		}
		
		void ButtonLastClick(object sender, EventArgs e)
		{
			this.listView1.Items.Clear();
			Page page = PageQueryDao.getProducts(totalPage);
			List<Product> ls = (List<Product>)page.ValueList;
			foreach(Product p in ls)
			{
				AddProduct(p);
				//Debug.WriteLine(p.Id);
			}
			
			this.labelMsg.Text=String.Format("记录数: {0:d}, \n每页{1:d}条记录, {2:d}/{3:d}页",
			                            page.TotalRecord,page.PageSize,page.CurPageIndex,page.TotalPage);
			
			curPageIndex=page.CurPageIndex;
			totalPage = page.TotalPage;
		}
		
		void ButtonDelClick(object sender, EventArgs e)
		{
			if(this.listView1.SelectedItems.Count<1)
			{
				MessageBox.Show("请选择要删除的记录","提示");
				return;
			}
			ListViewItem lvi = this.listView1.SelectedItems[0];
			
			int id = Int32.Parse(lvi.SubItems[0].Text);
			ProductDao.deleteById(id);
			
			lvi.Remove();
			this.textBoxId.Text="";
			this.textBoxName.Text="";
			this.textBoxPrice.Text="";
			this.textBoxQuantity.Text="";
			this.dateTimePicker1.Value=DateTime.Now;
			
		}
		
		void ButtonSaveClick(object sender, EventArgs e)
		{
			//
			StringBuilder strErrorMsg = new StringBuilder();
			if(!Validator.checkRequired(this.textBoxName.Text))
			{
				strErrorMsg.Append("名称不能为空\n");
			}
			if(!Validator.checkPositiveInteger(this.textBoxQuantity.Text))
			{
				strErrorMsg.Append("数量必须是正整数或零\n");
			}
			if(!Validator.checkDouble(this.textBoxPrice.Text))
			{
				strErrorMsg.Append("单价必须是浮点数\n");
			}
			if(strErrorMsg.Length>0)
			{
				MessageBox.Show(strErrorMsg.ToString(),"出错提示");
				return;
			}
			//
			Product p= new Product();
			p.Id=Int32.Parse(this.textBoxId.Text.Trim());
			p.Name = this.textBoxName.Text.Trim();
			p.Quantity = Int32.Parse(this.textBoxQuantity.Text.Trim());
			p.Price = Double.Parse(this.textBoxPrice.Text.Trim());
			p.SaleDate=this.dateTimePicker1.Value;
			p.CheckedFlag=this.radioButtonChecked.Checked;
			ProductDao.update(p);
			//
			curLvi.SubItems[1].Text=p.Name;
			curLvi.SubItems[2].Text=p.Quantity.ToString();
			curLvi.SubItems[3].Text=p.Price.ToString();
			curLvi.SubItems[4].Text=p.SaleDate.ToString();
			curLvi.SubItems[5].Text=p.CheckedFlag.ToString();			
		}
		
		void OnSelectedIndexChanged(object sender, EventArgs e)
		{
			if(this.listView1.SelectedItems.Count<1)
			{
				return;
			}
			ListViewItem lvi = this.listView1.SelectedItems[0];
			this.textBoxId.Text =lvi.SubItems[0].Text;
			this.textBoxName.Text=lvi.SubItems[1].Text;
			this.textBoxQuantity.Text=lvi.SubItems[2].Text;
			this.textBoxPrice.Text=lvi.SubItems[3].Text;
			this.dateTimePicker1.Text=lvi.SubItems[4].Text;
			if(lvi.SubItems[5].Text=="False")
			{
				this.radioButtonChecked.Checked=false;
				this.radioButtonUnchecked.Checked=true;
			}else
			{
				this.radioButtonChecked.Checked=true;
				this.radioButtonUnchecked.Checked=false;
			}
			
			curLvi=lvi;
			
		}
	}
}
